#!/usr/bin/python

import os, sys

jpg_dir = "."
if len(sys.argv) > 1:
	jpg_dir = sys.argv[1]
raw_dir = os.path.join(jpg_dir, "raw")
verbose	= True

def is_jpg(fn):
	return fn.lower().endswith(".jpg")
	
def is_raw(fn):
	return fn.lower().endswith(".cr2")

def fn_base(fn):
	return fn.rsplit(".", 1)[0]

#jpg = set( filter(is_jpg, os.listdir( jpg_dir )) )
#raw = set( filter(is_raw, os.listdir( raw_dir )) )
jpg = set( map(fn_base, filter(is_jpg, os.listdir( jpg_dir ))) )
raw = set( map(fn_base, filter(is_raw, os.listdir( raw_dir ))) )

remove = raw.difference(jpg)

if verbose:
	print "removing CR2 files", remove
	answer = raw_input("y/N? ")
	if answer not in "y Y yes Yes".split():
		sys.exit(-1)

for fn in remove:
	for ext in [".CR2", ".cr2"]:
		full_name = os.path.join(raw_dir, fn) + ext
		if os.path.exists(full_name):
			print "removing", full_name
			try:
				os.remove(full_name)
			except OSError, e:
				print e

